package com.pl.lc.user.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pl.lc.common.param.PayAdd;
import com.pl.lc.common.param.UserVipAdd;
import com.pl.lc.common.vo.R;
import com.pl.lc.core.alipay.AliPayUtil;
import com.pl.lc.user.entity.MoneyLog;
import com.pl.lc.user.entity.PayLog;
import com.pl.lc.user.entity.Wallet;
import com.pl.lc.user.mapper.MoneyLogMapper;
import com.pl.lc.user.mapper.PayLogMapper;
import com.pl.lc.user.mapper.WalletMapper;
import com.pl.lc.user.service.PayLogService;
import com.pl.lc.user.service.WalletService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 6.用户钱包表 服务实现类
 * </p>
 *
 * @author pl
 * @since 2024-06-11
 */
@Service
public class WalletServiceImpl extends ServiceImpl<WalletMapper, Wallet> implements WalletService {
    @Autowired
    private WalletMapper walletMapper;
    @Autowired
    private MoneyLogMapper moneyLogMapper;
    @Autowired
    private PayLogMapper payLogMapper;

    /**
     * 扣减余额
     *
     * @param userId
     * @param money
     * @return
     */
    @Override
    public boolean descBalance(Integer userId, Integer money) {
        //    查询是否还有余额
        QueryWrapper<Wallet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        Wallet wallet = walletMapper.selectOne(queryWrapper);
        //如果账户余额为空或者小于消费的金额money
        if (wallet == null && wallet.getMoney() < money) {
            return false;
        }
        //    削减余额
        wallet.setMoney(wallet.getMoney() - money);
        int i = walletMapper.updateById(wallet);
        if (i <= 0) {
            return false;
        }
        //    记录消减余额的日志
        MoneyLog moneyLog = new MoneyLog();
        moneyLog.setUserId(userId);
        moneyLog.setMoney(money);
        //减少余额
        moneyLog.setType(2);
        moneyLog.setFlag("会员等级");
        moneyLog.setInfo("购买会员");
        moneyLog.setCtime(new Date());
        moneyLogMapper.insert(moneyLog);

        return true;

    }

    @Override
    public R createWalletByZfb(PayAdd payAdd) {
        //记录充值日志
        PayLog payLog = new PayLog();
        float money = payAdd.getMoney() * 100;
        payLog.setUserId(payAdd.getUserId())
                .setPayType(2)
                .setMoney((int) money)
                .setTitle("支付宝充值");
        String no = "z" + System.currentTimeMillis() + RandomUtil.randomNumbers(6);
        payLog.setNo(no);
        // `status` int(11) DEFAULT NULL COMMENT '支付状态：1.未支付 2.已支付 3.取消支付 4.超时支付 5.退款 ',
        //  `ctime` datetime DEFAULT NULL COMMENT '创建时间',
        //  `utime` datetime DEFAULT NULL COMMENT '更新时间',
        payLog.setStatus(1);
        payLog.setCtime(new Date());
        payLog.setUtime(new Date());
        int num = payLogMapper.insert(payLog);

        if (num>0){

            // 调用支付宝支付
            String html = AliPayUtil.createPay(no, money, "爱贝充值");

            return R.ok(html);// 前端看到支付宝 界面
        }
        return R.fail("支付失败");
    }

    @Override
    public PayLog buyVipByZfb(UserVipAdd userVipAdd) {
        //记录充值日志
        PayLog payLog = new PayLog();
        float money = userVipAdd.getMoney() * 100;
        payLog.setUserId(userVipAdd.getUserId())
                .setPayType(2)
                .setMoney((int) money)
                .setTitle("支付宝充值Vip");
        String no = "v" + System.currentTimeMillis() + RandomUtil.randomNumbers(6);
        payLog.setNo(no);
        // `status` int(11) DEFAULT NULL COMMENT '支付状态：1.未支付 2.已支付 3.取消支付 4.超时支付 5.退款 ',
        //  `ctime` datetime DEFAULT NULL COMMENT '创建时间',
        //  `utime` datetime DEFAULT NULL COMMENT '更新时间',
        payLog.setStatus(1);
        payLog.setCtime(new Date());
        payLog.setUtime(new Date());
        int num = payLogMapper.insert(payLog);

        if (num>0){
        return payLog;}
        return null;
    }
}
